#include "TandemTimer.hpp"

/** 
* Instantiate non counting timer.
*/
TandemTimer::TandemTimer(){
    periodOne = periodTwo = 0;
    switchCounter = 0;
    switchCount = 1;
}

/** 
* Set timer periods and the switch count.
*/
void TandemTimer::setPeriods(byte s, unsigned int one, unsigned int two){
    switchCount = s;
    periodOne = one;
    periodTwo = two;
    restart();
}

/** 
* Clear time and switch counter.
*/
void TandemTimer::restart(){
    startTime = 0;
    currentPeriod = periodOne;
    switchCounter = 0;
}

/** 
* Check if period time has elapsed. Starts next period timing if true.
*/
bool TandemTimer::passed(unsigned int time){
    if(time - startTime > currentPeriod){
        
        if(switchCounter >= switchCount){
            currentPeriod = periodTwo;
        }else{
            switchCounter++;
        }
        
        startTime = time;
        return true;
    }
    return false;
}